package com.book.print.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.client.token.AccessTokenProviderChain;
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider;
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
import org.springframework.security.oauth2.common.AuthenticationScheme;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;

import java.util.Arrays;

/**
 * Created by wangxiangyun on 2018/9/7.
 */

@EnableOAuth2Client
@Configuration
public class Oauth2ClientConfig {
    
    @Value("${accessTokenUri}")
    private String accessTokenUri;
    
    @Value("${userAuthorizationUri}")
    private String userAuthorizationUri;
    @Autowired
    private OAuth2ClientContext oauth2Context;
    @Bean
    public OAuth2ProtectedResourceDetails bookResourceDetail() {
        AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
        details.setId("book-service");
        details.setClientId("book_client");
        details.setClientSecret("book_client");
        details.setAccessTokenUri(accessTokenUri);
        details.setUserAuthorizationUri(userAuthorizationUri);
        details.setScope(Arrays.asList("read", "write"));
        details.setClientAuthenticationScheme(AuthenticationScheme.form);
//        details.setPreEstablishedRedirectUri("http://www.baidu.com");
        return details;
    }
    
   
    @Bean
    public OAuth2RestTemplate bookRestTemplate() {
        return new OAuth2RestTemplate(bookResourceDetail(), oauth2Context);
    }
    
    
}